using System;
using System.Collections.Generic;
using System.Text;

namespace Wrath.Xml.Objects
{
    public class FrameDefinition
    {
        public class Rectangle
        {
            public int X;
            public int Y;
            public int Width;
            public int Height;
        }

        private string name;

	    public string Name
	    {
		    get { return name; }
		    set { name = value; }
	    }

        private string bitmap;

        public string Bitmap
        {
            get { return bitmap; }
            set { bitmap = value; }
        }

        private Rectangle topLeft;

        public Rectangle TopLeft
        {
            get { return topLeft; }
            set { topLeft = value; }
        }

        private Rectangle top;

        public Rectangle Top
        {
            get { return top; }
            set { top = value; }
        }

        private Rectangle topRight;

        public Rectangle TopRight
        {
            get { return topRight; }
            set { topRight = value; }
        }

        private Rectangle left;

        public Rectangle Left
        {
            get { return left; }
            set { left = value; }
        }

        private Rectangle middle;

        public Rectangle Middle
        {
            get { return middle; }
            set { middle = value; }
        }

        private Rectangle right;

        public Rectangle Right
        {
            get { return right; }
            set { right = value; }
        }

        private Rectangle bottomLeft;

        public Rectangle BottomLeft
        {
            get { return bottomLeft; }
            set { bottomLeft = value; }
        }

        private Rectangle bottom;

        public Rectangle Bottom
        {
            get { return bottom; }
            set { bottom = value; }
        }

        private Rectangle bottomRight;

        public Rectangle BottomRight
        {
            get { return bottomRight; }
            set { bottomRight = value; }
        }

        private Rectangle decal;

        public Rectangle Decal
        {
            get { return decal; }
            set { decal = value; }
        }

        public FrameDefinition()
        {
        }

        public FrameDefinition(Element frameDef)
        {
            Attribute nameAttribute = frameDef.FindAttribute("name");
            if (nameAttribute != null)
            {
                name = nameAttribute.Value.Substring(0);
            }

            Element element = frameDef.FindFirstElement("bitmap", false);
            if (element != null)
            {
                Attribute fileAttribute = element.FindAttribute("file");
                if (fileAttribute != null)
                {
                    bitmap = fileAttribute.Value.Substring(0);
                }
            }

            element = frameDef.FindFirstElement("topleft", false);
            if (element != null)
            {
                Attribute attribute = element.FindAttribute("rect");
                if (attribute != null)
                {
                    string rect = attribute.Value;
                    string[] bounds = rect.Split(',');

                    if (bounds.Length == 4)
                    {
                        topLeft = new Rectangle();
                        topLeft.X = Convert.ToInt32(bounds[0]);
                        topLeft.Y = Convert.ToInt32(bounds[1]);
                        topLeft.Width = Convert.ToInt32(bounds[2]);
                        topLeft.Height = Convert.ToInt32(bounds[3]);
                    }
                }
            }

            element = frameDef.FindFirstElement("top", false);
            if (element != null)
            {
                Attribute attribute = element.FindAttribute("rect");
                if (attribute != null)
                {
                    string rect = attribute.Value;
                    string[] bounds = rect.Split(',');

                    if (bounds.Length == 4)
                    {
                        top = new Rectangle();
                        top.X = Convert.ToInt32(bounds[0]);
                        top.Y = Convert.ToInt32(bounds[1]);
                        top.Width = Convert.ToInt32(bounds[2]);
                        top.Height = Convert.ToInt32(bounds[3]);
                    }
                }
            }

            element = frameDef.FindFirstElement("topright", false);
            if (element != null)
            {
                Attribute attribute = element.FindAttribute("rect");
                if (attribute != null)
                {
                    string rect = attribute.Value;
                    string[] bounds = rect.Split(',');

                    if (bounds.Length == 4)
                    {
                        topRight = new Rectangle();
                        topRight.X = Convert.ToInt32(bounds[0]);
                        topRight.Y = Convert.ToInt32(bounds[1]);
                        topRight.Width = Convert.ToInt32(bounds[2]);
                        topRight.Height = Convert.ToInt32(bounds[3]);
                    }
                }
            }

            element = frameDef.FindFirstElement("left", false);
            if (element != null)
            {
                Attribute attribute = element.FindAttribute("rect");
                if (attribute != null)
                {
                    string rect = attribute.Value;
                    string[] bounds = rect.Split(',');

                    if (bounds.Length == 4)
                    {
                        left = new Rectangle();
                        left.X = Convert.ToInt32(bounds[0]);
                        left.Y = Convert.ToInt32(bounds[1]);
                        left.Width = Convert.ToInt32(bounds[2]);
                        left.Height = Convert.ToInt32(bounds[3]);
                    }
                }
            }

            element = frameDef.FindFirstElement("middle", false);
            if (element != null)
            {
                Attribute attribute = element.FindAttribute("rect");
                if (attribute != null)
                {
                    string rect = attribute.Value;
                    string[] bounds = rect.Split(',');

                    if (bounds.Length == 4)
                    {
                        middle = new Rectangle();
                        middle.X = Convert.ToInt32(bounds[0]);
                        middle.Y = Convert.ToInt32(bounds[1]);
                        middle.Width = Convert.ToInt32(bounds[2]);
                        middle.Height = Convert.ToInt32(bounds[3]);
                    }
                }
            }

            element = frameDef.FindFirstElement("right", false);
            if (element != null)
            {
                Attribute attribute = element.FindAttribute("rect");
                if (attribute != null)
                {
                    string rect = attribute.Value;
                    string[] bounds = rect.Split(',');

                    if (bounds.Length == 4)
                    {
                        right = new Rectangle();
                        right.X = Convert.ToInt32(bounds[0]);
                        right.Y = Convert.ToInt32(bounds[1]);
                        right.Width = Convert.ToInt32(bounds[2]);
                        right.Height = Convert.ToInt32(bounds[3]);
                    }
                }
            }

            element = frameDef.FindFirstElement("bottomleft", false);
            if (element != null)
            {
                Attribute attribute = element.FindAttribute("rect");
                if (attribute != null)
                {
                    string rect = attribute.Value;
                    string[] bounds = rect.Split(',');

                    if (bounds.Length == 4)
                    {
                        bottomLeft = new Rectangle();
                        bottomLeft.X = Convert.ToInt32(bounds[0]);
                        bottomLeft.Y = Convert.ToInt32(bounds[1]);
                        bottomLeft.Width = Convert.ToInt32(bounds[2]);
                        bottomLeft.Height = Convert.ToInt32(bounds[3]);
                    }
                }
            }

            element = frameDef.FindFirstElement("bottom", false);
            if (element != null)
            {
                Attribute attribute = element.FindAttribute("rect");
                if (attribute != null)
                {
                    string rect = attribute.Value;
                    string[] bounds = rect.Split(',');

                    if (bounds.Length == 4)
                    {
                        bottom = new Rectangle();
                        bottom.X = Convert.ToInt32(bounds[0]);
                        bottom.Y = Convert.ToInt32(bounds[1]);
                        bottom.Width = Convert.ToInt32(bounds[2]);
                        bottom.Height = Convert.ToInt32(bounds[3]);
                    }
                }
            }

            element = frameDef.FindFirstElement("bottomright", false);
            if (element != null)
            {
                Attribute attribute = element.FindAttribute("rect");
                if (attribute != null)
                {
                    string rect = attribute.Value;
                    string[] bounds = rect.Split(',');

                    if (bounds.Length == 4)
                    {
                        bottomRight = new Rectangle();
                        bottomRight.X = Convert.ToInt32(bounds[0]);
                        bottomRight.Y = Convert.ToInt32(bounds[1]);
                        bottomRight.Width = Convert.ToInt32(bounds[2]);
                        bottomRight.Height = Convert.ToInt32(bounds[3]);
                    }
                }
            }

            ////
            element = frameDef.FindFirstElement("decal", false);
            if (element != null)
            {
                Attribute attribute = element.FindAttribute("rect");
                if (attribute != null)
                {
                    string rect = attribute.Value;
                    string[] bounds = rect.Split(',');

                    if (bounds.Length == 4)
                    {
                        decal = new Rectangle();
                        decal.X = Convert.ToInt32(bounds[0]);
                        decal.Y = Convert.ToInt32(bounds[1]);
                        decal.Width = Convert.ToInt32(bounds[2]);
                        decal.Height = Convert.ToInt32(bounds[3]);
                    }
                }
            }
        }
    }
}
